const express = require("express");
const router = express.Router();
//路由只管匹配接口 逻辑处理放在controller
const path = require("path")
//把控制器里的add方法拿过来

//图片上传
//1.cnpm i multer --save
//2.require("multer")
const multer = require("multer");
//3.定义存储磁盘

var storage = multer.diskStorage({ //设置图片储存路径和图片命名
    destination: function (req, file, cb) { //设置存储路径的方法
        cb(null, path.resolve(__dirname, "../public/uploads"))
    },
    filename: function (req, file, cb) {
        //fieldname 前端name指定的值
        // <input name="haha" > file.fieldname="haha"
        // originalname 计算机上面的文件名
        console.log(file.originalname+"图片名称");//获取文件扩展名 再拼接到路径
        //解析文件扩展名
        // timg.q3.jsfg.jpg     var index = lastIndexOf(".")
        // str.subString(index)
        // 正则匹配  exec()
        var extName = /\.[^\.]+$/.exec(file.originalname)[0];//匹配任何的后缀名
        // /\.(jpg|png|gif|jpeg)+$/
        var fileName = file.fieldname + '-' + Date.now() + extName
        //把图片地址存到数据库
        var imgAddress = "http://127.0.0.1:3000/uploads/" + fileName;
        req.imgAddress = imgAddress;
        cb(null, fileName)
    }
})

var upload = multer({ storage: storage })

router.get("/", (req, res) => {
    res.send("3213213")
})

const positionController = require("../controllers/positionController")
router.post('/add', upload.single("companyLogo"), positionController.positionAdd);
//获取列表的接口
router.get("/find", positionController.positionFind) //使用控制器里面的逻辑

router.get("/del", positionController.positionDel)

router.post('/update', upload.single("companyLogo"), positionController.positionUpdate)

module.exports = router;